package com.bootdo.haojiangboutils;

import com.bootdo.common.annotation.NotColumn;

import java.lang.reflect.Field;
/**
 * 对象属性拷贝工具类
 　　* @author 郝江波
 　　* @date 2019/9/10 16:23
 　　*/
public class ObjectCopy <T>{
    /**
     * 当前对象拷贝到目标对象
     * @param now
     * @param target
     * @return
     */
    public T objCopyByN2T(T now ,T target){
        return objCopyByT2N(now,target);
    }

    /**
     * 目标对象拷贝到当前对象
     * @param target
     * @param now
     * @return
     */
    public T objCopyByT2N(T target ,T now){
        Field[] fields =   target.getClass().getDeclaredFields();
        try {
            for(Field field : fields){
                field.setAccessible(true);
                NotColumn notColumn =  field.getAnnotation(NotColumn.class);
                if(null == notColumn){
                    Object r = field.get(target);
                    if(null != r){
                        field.set(now, r);
                    }
                }
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return  now;
    }

}
